/*
 * DestinationScreen.java
 *
 */
package com.gpsnav.dao;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import java.io.IOException;
import com.gpsnav.bo.Options;

public class OptionsDAO {

    String rsName = "GPSProject";

    public OptionsDAO() {
    }


    public Options loadOptions() {

        int recordID = 1; // the record ID to read from
        Options o = new Options();
        
        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );
            if (rs.getNumRecords() >0) {
                byte[] data = rs.getRecord( recordID );
                o.fromByteArray( data );
            }
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
        }
        catch( IOException e ){
            // handle the IO error here
        }
        return o;
    }

    public void storeOptions(Options o) {
        
        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );
            byte[] data = o.toByteArray();
            if (rs.getNumRecords() == 0) {
                rs.addRecord( data, 0, data.length );
            } else {
                rs.setRecord( 1, data, 0, data.length );
            }
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
        }
        catch( IOException e ){
            // handle the IO error here
        }

    }
    
}

